package com.lxp.tksys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxp.tksys.common.CustomException;
import com.lxp.tksys.entity.Category;
import com.lxp.tksys.entity.Dish;
import com.lxp.tksys.entity.Employee;
import com.lxp.tksys.entity.Setmeal;
import com.lxp.tksys.mapper.CategoryMapper;
import com.lxp.tksys.mapper.EmployMapper;
import com.lxp.tksys.service.CategoryService;
import com.lxp.tksys.service.DishService;
import com.lxp.tksys.service.EmployService;
import com.lxp.tksys.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    //id删除，查询是否关联菜品、套餐，都没有既可以正常删除
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);         //根据CategoryId查对应的菜品
        int count = dishService.count(dishLambdaQueryWrapper);
        if(count>0){
            throw new CustomException("当前分类关联了菜品不能删除！");
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);    //根据CategoryId查对应的套餐
        int count1 = setmealService.count(setmealLambdaQueryWrapper);
        if(count1>0){
            throw new CustomException("当前分类关联了套餐不能删除！");
        }

        //都没有关联
        super.removeById(id);

    }
}
